package com.dys.dao.impl;

import com.dys.dao.AccountDAO;
import com.dys.entity.Account;
import com.dys.rowmapper.impl.AccountRowMapper;
import com.dys.util.DaoUtils;
import com.dys.util.DatabaseUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @className: com.dys.dao.impl.AccountDAOImpl
 * @description: AccountDAO的实现类：
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/13 下午9:13
 */
public class AccountDAOImpl02 implements AccountDAO {
    private final DaoUtils<Account> daoUtils = new DaoUtils<>();

    @Override
    public Account findAccountByCardNo(String cardNo) throws SQLException {
        // 1、编写SQL语句
        String sql = "select * from account where card_no=?";

        // 2、调用DaoUtils
        List<Account> list = daoUtils.commonSelect(sql, new AccountRowMapper(), cardNo);

        // 3、返回结果
        return list != null && !list.isEmpty() ? list.get(0) : null;
    }

    @Override
    public int updateAccountByCardNo(Account account) throws SQLException {
        // 1、编写SQL语句
        String sql = "update account set money=? where card_no=?";

        // 2、调用DaoUtils
        int count = daoUtils.commonUpdate(sql, account.getMoney(), account.getCardNo());

        // 3、返回结果
        return count;
    }
}
